// <license>
//  Exception4Net is exception handler framework for dot net.
//  Copyright (C) 2002  sijingnan
//  
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; either version 2
//  of the License, or (at your option) any later version.
//  
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
// 
// </license>

namespace Exception4Net.Config
{
	using System;
	using System.Configuration;
	using System.Xml;

	/// <summary>
	/// Class to register for the exception4net section of the configuration file
	/// </summary>
	/// <remarks>
	/// The exception4net section of the configuration file needs to have a section
	/// handler registered. This is the section handler used. 
	/// It read all values from the xml node,and return it's self which contains values.
	/// </remarks>
	/// <example>
	/// Example of registering the section handler
	/// set the file "C:/abc/resource/exceptions.resource" as the exception resource.
	/// you can provide several resource file with the same base name for different locale
	/// such as
	/// "C:/abc/resource/exceptions.resource"
	/// "C:/abc/resource/exceptions.en-US.resource"
	/// "C:/abc/resource/exceptions.zh-CN.resource"
	/// "C:/abc/resource/exceptions.zh-TW.resource"
	/// <code>
	/// &lt;?xml version="1.0" encoding="utf-8" ?&gt;
	/// &lt;configuration&gt;
	///		&lt;configSections&gt;
	///			&lt;section name="exception4net" type="Exception4Net.Config.Exception4NetConfigurationSectionHandler,Exception4Net" /&gt;
	///		&lt;/configSections&gt;
	///		&lt;exception4net&gt;
	///			&lt;resourcepath&gt;
	///				C:/abc/resource/
	///			&lt;/resourcepath&gt;
	///			&lt;resourcename&gt;
	///				exceptions
	///			&lt;/resourcepath&gt;
	///		&lt;/exception4net&gt;
	/// &lt;/configuration&gt;
	/// </code>
	/// </example>
	public class Exception4NetConfigurationSectionHandler : IConfigurationSectionHandler
	{
		#region Constants
			public const String CONFIGURATION_TAG = "exception4net";
			private const String RESOURCE_PATH_TAG = "resourcepath";
			private const String RESOURCE_NAME_TAG = "resourcename";
		#endregion

		private String resourcePath = null;
		private String resourceName = null;

		/// <summary>
		/// The Default constructor
		/// </summary>
		public Exception4NetConfigurationSectionHandler()
		{
		}

		public object Create(object parent,	object configContext,XmlNode section) 
		{
			//test the root name
			if (!section.Name.Equals(CONFIGURATION_TAG))
				throw new ConfigurationException("Unknown Element",section);

			if (section.HasChildNodes)
			{
				XmlNodeList children = section.ChildNodes;

				int count = children.Count;

				for(int i=0;i<count;i++)
				{
					XmlNode node = children.Item(i);

					if (node.Name.Equals(RESOURCE_PATH_TAG))
					{
						//resource path
						if (node.HasChildNodes)
                            this.resourcePath = node.FirstChild.Value;
						else
							this.resourcePath = node.Value;
					}
					else if (node.Name.Equals(RESOURCE_NAME_TAG))
					{
						//resource base name
						if (node.HasChildNodes)
							this.resourceName = node.FirstChild.Value;
						else
							this.resourceName = node.Value;
					}
				}
			}

			return this;
		}

		/// <summary>
		/// return the resource path for exception4net.
		/// </summary>
		public String ResourcePath
		{
			get
			{
				return this.resourcePath;
			}
		}

		/// <summary>
		/// return the resource name for exception4net
		/// </summary>
		public String ResourceName
		{
			get
			{
				return this.resourceName;
			}
		}
	}
}
